package com.hust.yuqian.utils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


/**
 * Created by mmutuyu on 14-1-10.
 */
public class ImageTools {

    /**
     * 获得原始图片的BufferedImage, 同时设置 UsefulData 中image_height, image_width数据
     * @param filePath 原始图片的路径
     * @return 原始图片的BufferedImage
     */
    public BufferedImage getImage(String filePath){
        BufferedImage bufImg = new BufferedImage(UsefulData.image_height, UsefulData.image_width, BufferedImage.TYPE_INT_ARGB);

        File file  = new File(filePath);
        if(!file.exists()){
            System.out.printf("In function 'getImage': %s cannot be found!\n", filePath);
            System.exit(0);
        }
        try {
            bufImg = ImageIO.read(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int height = bufImg.getHeight();
        int width = bufImg.getWidth();

        if(height != UsefulData.WATERMARK_HEIGHT && width != UsefulData.WATERMARK_WIDTH){
            UsefulData.image_height = height;
            UsefulData.image_width = width;
        }
        return bufImg;
    }

    /**
     * 获取图片RGB数组
     * @param bufImg 原始图片的BufferedImage
     * @return 对应图片的 rgb 值
     */
    public int[][] getImageRGB(BufferedImage bufImg){
        int height = bufImg.getHeight();
        int width = bufImg.getWidth();
        int[][] rgb = new int[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j <  width; j++) {
                rgb[i][j] = bufImg.getRGB(i, j) & 0xFFFFFF;
            }
        }
        return rgb;
    }

    /**
     * 获取图片B数组
     * @param bufImg 原始图片的BufferedImage
     * @return  对应图片的 b 值
     */
    public int[][] getImageB(BufferedImage bufImg) {
        int height = bufImg.getHeight();
        int width = bufImg.getWidth();
        int[][] b = new int[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j <  width; j++) {
                b[i][j] = bufImg.getRGB(i, j) & 0xFF;//只取得 rgb 中的 b 值
            }
        }
        return b;
    }

    /**
     *
     * @param originalImage 原始图片的BufferedImage
     * @param b 对应图片的 b 值, 这里一般是加入了水印后的 b 值
     * @return 修改b 值后的BufferedImage
     */
    public BufferedImage changeImageRGB(BufferedImage originalImage, int [][] b){
        int red = 0, green = 0, blue = 0, rgb = 0;
        int height = originalImage.getHeight();
        int width = originalImage.getWidth();

        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                Object data = originalImage.getRaster().getDataElements(i, j, null);//获取该点像素，并以object类型表示
                red = originalImage.getColorModel().getRed(data);
                green = originalImage.getColorModel().getGreen(data);
                blue = b[i][j];//blue 设置为加入过水印的 b 值

                /*
                这里将r、g、b再转化为rgb值，
                rgb最大为16777215，当大于这个值时，应减去255*255*255即16777216
                */
                rgb = (red*256 + green)*256+blue;
                if(rgb>16777215){
                    rgb = rgb - 16777216;
                }

                originalImage.setRGB(i, j, rgb);
            }
        }

        return originalImage;
    }

    public void WriteImage(BufferedImage originalImage, String name) throws Exception{
        StringBuilder path = new StringBuilder(System.getProperty("user.dir"))
                .append(System.getProperty("file.separator")).append(name);
        File file = new File(path.toString());
        ImageIO.write(originalImage, "jpg", file);
    }

}
